/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.splitor;

import cn.ac.iie.di.global.GlobalParas;
import cn.ac.iie.di.tools.FileTools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author lenovo-pc
 */
public class JsonSplitor implements Runnable {
    
    String dir = null;
//    String desdir = null;
    ArrayBlockingQueue dataQueue = null;
    String FileString = null;
    
    static Logger logger = null;
    
    static {
        logger = org.apache.log4j.Logger.getLogger(JsonSplitor.class.getName());
    }
    
    public static void main(String[] args) {
//        Thread t = new Thread(new JsonSplitor("D:\\123\\111", "D:\\123\\111", "xx", new ArrayBlockingQueue<String>(10)));
//        t.start();
    }
    
    public JsonSplitor(String dir, String filetype, ArrayBlockingQueue dataQueue) {
        this.dir = dir;
        this.dataQueue = dataQueue;
//        this.desdir = desdir;
        this.FileString = filetype;
    }
    
    public void run() {
        while (true) {
            try {
//                logger.info("the json parser dir is " + dir);
//                List<File> sigFiles = FileTools.getFiles(new File(dir), "ok");
//
//                logger.info("current ok files " + sigFiles);
                List<String> okFiles = new ArrayList<>();
                for (int i = 0; i < 5; i++) {
                    String str = GlobalParas.jsonQueue.poll(500, TimeUnit.MILLISECONDS);
                    if (str == null) {
                        break;
                    }
                    okFiles.add(str);
                }
                for (String sigFile : okFiles) {
                    String[] fileName = sigFile.split("=");
                    String dataFilePath = GlobalParas.TMP_NEWS + File.separator + fileName[0] + File.separator + fileName[1].split(".ok")[0] + ".json";
                    logger.info("file:" + dataFilePath);
                    readFileByLines(dataFilePath, fileName[0], FileString);
                }
//                Thread.sleep(3000);
            } catch (Exception e) {
                logger.error("JsonSplitor error : " + e, e);
            }
            
        }
    }
    
    public void readFileByLines(String fileName, String zipDir, String FileType) {
        
        BufferedReader reader = null;
        String filetype = null;
        filetype = FileType;
        try {
            long begin = System.currentTimeMillis();
            //System.out.println("以行为单位读取文件内容，一次读一整行：");
            //reader = new BufferedReader(new FileReader(fileName));
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "UTF-8"));
            String tempString = null;
//            int line = 0;
            // 一次读入一行，直到读入null为文件结束  
            StringBuffer sb = new StringBuffer();
            while ((tempString = reader.readLine()) != null) {
                sb.append(tempString.trim());
            }
            if (!sb.toString().trim().isEmpty()) {
                try {
                    while (!dataQueue.offer(zipDir + "=" + sb.toString())) {
                        Thread.sleep(1000);
                        logger.warn("Put Json Array Block buffer Fail!");
                    }
                    
                    logger.info("the ataQueue.offer" + zipDir + "=" + sb.toString());
                    logger.debug("dataQueue.offer.size(): " + dataQueue.size());
                } catch (InterruptedException ex) {
                }
//                line++;
            }
            reader.close();
//            logger.info("There is " + line + " in " + fileName + "！ ");
            //将文件移动走，删除.ok文件和解压文件，移动.gz和.sig文件
            //要是IMSG和GMSG直接移走，USER需要创建文件后缀

//            FileTools.moveFileToNewPath(fileName, newFileName);
            long end = System.currentTimeMillis();
            System.out.println("consume " + (end - begin) + " ms");
        } catch (Exception e) {
            logger.error("the error is :" + e, e);
        }
    }
}
